package com.shujia.flink.async;

import org.apache.commons.dbcp2.BasicDataSource;

import java.sql.Connection;
import java.sql.SQLException;

public class JDBCUtil {

    private static BasicDataSource poll;

    static {
        //创建连接池
        poll = new BasicDataSource();
        poll.setUsername("root");
        poll.setPassword("123456");
        poll.setDriverClassName("com.mysql.jdbc.Driver");
        poll.setUrl("jdbc:mysql://node1:3306/student");
        poll.setMinIdle(50); //最大连接数
        poll.setMinIdle(2);//最小连接数
        poll.setInitialSize(5); // 初始连接数
    }

    public static Connection getConnection() {


        System.out.println("当前活跃的线程：" + poll.getNumActive());
        Long start = System.currentTimeMillis();

        Connection con = null;
        //从连接池中获取连接
        try {
            con = poll.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        Long end = System.currentTimeMillis();
        System.out.println("创建连接花费时间：" + (end - start));

        return con;
    }
}
